import React, {useEffect, useState} from 'react'
import {Form} from 'antd'
import type {UploadFile} from 'antd/es/upload/interface'
import styles from './index.less'
import CustomUpload from '@/components/Upload'
import OtherUpload from './OtherUpload'

const qualifications = (props: any) => {
	const {required, urlData, setUrlData, type, enclosureList, supplierType} =
		props
	const [fileList, setFileList] = useState<UploadFile[]>([])
	const callBack = (value: string | number, key: any) => {
		const data = {...urlData}
		data[value] = key
		setUrlData(data)
	}
	const uploadDom = (
		targetAttr: number,
		name: string,
		fileFormat?: string,
		attachmentSize?: number,
	) => {
		const uploadProps: any = {
			key: targetAttr,
			modelName: 'spAccess',
			targetAttr,
			fileFormat,
			attachmentSize,
			uploadType: 'devUpload',
			callback: (key: string) => {
				callBack(name, key)
			},
		}
		if (type !== 'add' && enclosureList?.length) {
			const newData = enclosureList?.find(
				(item: {targetAttr: number}) => item?.targetAttr === targetAttr,
			)
			if (newData) {
				uploadProps.fileList = [
					{
						uid: newData?.targetAttr,
						name: newData?.filename,
						url: newData?.url,
					},
				]
			}
		}
		return (
			<>
				<CustomUpload {...uploadProps} />{' '}
			</>
		)
	}
	useEffect(() => {
		const data = {...urlData}
		fileList?.forEach((item, index) => {
			if (item?.response) {
				data[`otherDocumentsUrl${index + 1}`] = item?.response?.data?.id
			} else {
				data[`otherDocumentsUrl${index + 1}`] = item?.uid
			}
			setUrlData(data)
		})
		if (fileList?.length === 0) {
			data.otherDocumentsUrl1 = ''
			data.otherDocumentsUrl2 = ''
			data.otherDocumentsUrl3 = ''
		} else if (fileList?.length === 1) {
			data.otherDocumentsUrl2 = ''
			data.otherDocumentsUrl3 = ''
		} else if (fileList?.length === 2) {
			data.otherDocumentsUrl3 = ''
		}
	}, [fileList])
	useEffect(() => {
		const arr: any = []
		if (enclosureList?.length) {
			enclosureList?.forEach((item: any) => {
				if (item?.targetAttr === 31) {
					arr.push({
						uid: item?.id,
						name: item?.filename,
						url: item?.url,
					})
				}
			})
			setFileList(arr)
		}
	}, [enclosureList])

	return (
		<div className={styles.qualifications}>
			<Form
				name="basic"
				autoComplete="off"
				disabled={type === 'view' || type === 'process'}>
				{supplierType === 'SP_GYS' ? (
					<table border="1" className={styles.uploadWrap}>
						<tr>
							<td className={required ? styles.required : ''}>
								营业执照
							</td>
							<td>
								{uploadDom(
									1,
									'businessLicenseUrl',
									'jpg,png',
									0.6,
								)}
							</td>
							<td className={required ? styles.required : ''}>
								开户许可证
							</td>
							<td>
								{uploadDom(
									2,
									'openingPermitUrl',
									'jpg,png',
									0.6,
								)}
							</td>
							<td>保密承诺函</td>
							<td>
								{uploadDom(
									3,
									'confidentialityCommitmentUrl',
									'jpg,png',
									0.6,
								)}
							</td>
						</tr>
						<tr>
							<td>法人身份证</td>
							<td>{uploadDom(4, 'corporateCardUrl')}</td>
							<td>企业法人征信报告</td>
							<td>{uploadDom(5, 'creditReportUrl')}</td>
							<td>廉洁声明</td>
							<td>{uploadDom(7, 'statementIntegrityUrl')}</td>
						</tr>
						<tr>
							<td>供应商道德行为规范承诺书</td>
							<td>
								{uploadDom(9, 'moralityStandardPromiseUrl')}
							</td>
							<td>经营场地证明及场地照片</td>
							<td>{uploadDom(10, 'certificationPhotosUrl')}</td>
							<td>保证金认款单截图</td>
							<td>{uploadDom(12, 'depositLetterUrl')}</td>
						</tr>
						<tr>
							<td colSpan={6}>
								<OtherUpload
									targetAttr={31}
									uploadType="DDUpload"
									urlName="otherDocumentsUrl"
									fileList={fileList}
									setFileList={setFileList}
									enclosureList={enclosureList}
									urlData={urlData}
									setUrlData={setUrlData}
								/>
							</td>
						</tr>
					</table>
				) : (
					<table border="1" className={styles.uploadWrap}>
						<tr>
							<td className={required ? styles.required : ''}>
								营业执照
							</td>
							<td>
								{uploadDom(
									1,
									'businessLicenseUrl',
									'jpg,png',
									0.6,
								)}
							</td>
							<td className={required ? styles.required : ''}>
								开户许可证
							</td>
							<td>
								{uploadDom(
									2,
									'openingPermitUrl',
									'jpg,png',
									0.6,
								)}
							</td>
							<td>保密承诺函</td>
							<td>
								{uploadDom(
									3,
									'confidentialityCommitmentUrl',
									'jpg,png',
									0.6,
								)}
							</td>
						</tr>
						<tr>
							<td>法人身份证</td>
							<td>{uploadDom(4, 'corporateCardUrl')}</td>
							<td>企业法人征信报告</td>
							<td>{uploadDom(5, 'creditReportUrl')}</td>
							<td>供应商登记表</td>
							<td>{uploadDom(6, 'registrationFormUrl')}</td>
						</tr>
						<tr>
							<td>廉洁声明</td>
							<td>{uploadDom(7, 'statementIntegrityUrl')}</td>
							<td>供应商道德行为规范</td>
							<td>{uploadDom(8, 'moralityStandardUrl')}</td>
							<td>供应商道德行为规范承诺书</td>
							<td>
								{uploadDom(9, 'moralityStandardPromiseUrl')}
							</td>
						</tr>
						<tr>
							<td>经营场地证明及场地照片</td>
							<td>{uploadDom(10, 'certificationPhotosUrl')}</td>
							<td>合同扫描件</td>
							<td>{uploadDom(11, 'contractScanningUrl')}</td>
							<td>保证金认款单截图</td>
							<td>{uploadDom(12, 'depositLetterUrl')}</td>
						</tr>
						<tr>
							<td colSpan={6}>
								<OtherUpload
									targetAttr={31}
									uploadType="DDUpload"
									urlName="otherDocumentsUrl"
									fileList={fileList}
									setFileList={setFileList}
									enclosureList={enclosureList}
									urlData={urlData}
									setUrlData={setUrlData}
								/>
							</td>
						</tr>
					</table>
				)}
			</Form>
		</div>
	)
}

export default qualifications
